//-----------------------------------------------------------------------------
// Bend2D - Physics Engine
// Roberto Bender <beto.bender@gmail.com>
// Description: 
// Date: 16/03/2010
//-----------------------------------------------------------------------------
#include "moveable.h"
using namespace bend2d;

Moveable::MoveableList::iterator& Moveable::GetMoveableIterator()
{
    return m_Iterator;
}

const Speed& Moveable::GetSpeed() const
{
    return m_Speed;
}

const Acceleration& Moveable::GetAcceleration() const
{
    return m_Acceleration;
}

const Angle& Moveable::GetAngle() const
{
    return m_Angle;
}

const Point& Moveable::GetPosition() const
{
    return m_Position;
}

void Moveable::SetSpeed(const Speed& speed)
{
    m_Speed = speed;
    m_NewSpeed = speed;
}

void Moveable::SetAcceleration(const Acceleration& acceleration)
{
    m_Acceleration = acceleration;
    m_NewAcceleration = acceleration;
}

void Moveable::SetExternalAcceleration(const Acceleration& acceleration)
{
    m_ExternalAcceleration = acceleration;
}

void Moveable::SetAngle(const Angle& angle)
{
    m_Angle = angle;
    m_NewAngle = angle;
}

void Moveable::SetPosition(const Point& point)
{
    m_Position = point;
    m_NewPosition = point;
}

void Moveable::Move(double timeSinceLastFrame)
{
    m_NewSpeed += (m_Acceleration + m_ExternalAcceleration + m_InstantAcceleration) * timeSinceLastFrame;
   
    m_NewPosition.SetX(m_NewPosition.GetX() + m_NewSpeed.GetX() * timeSinceLastFrame);
    m_NewPosition.SetY(m_NewPosition.GetY() + m_NewSpeed.GetY() * timeSinceLastFrame);
    m_InstantAcceleration = Acceleration();
}

void Moveable::Moved()
{
    m_Position = m_NewPosition;
    m_Speed = m_NewSpeed;
    m_Acceleration = m_NewAcceleration;

    TestSpeedContraints();
}

void Moveable::TestSpeedContraints()
{
    if(m_MaxSpeed.GetMag() > 0)
    {
        if(m_NewSpeed.GetMag() > m_MaxSpeed.GetMag())
            m_NewSpeed.SetMag(m_MaxSpeed.GetMag());
    }
}

void Moveable::ResetMove()
{
    m_ExternalAcceleration = Acceleration();
}

const Speed& Moveable::GetNewSpeed() const
{
	return m_NewSpeed;
}

const Acceleration& Moveable::GetNewAcceleration() const
{
	return m_NewAcceleration;
}

const Acceleration& Moveable::GetExternalAcceleration() const
{
    return m_ExternalAcceleration;
}

const Angle& Moveable::GetNewAngle() const
{
	return m_NewAngle;
}

const Point& Moveable::GetNewPosition() const
{
	return m_NewPosition;
}

void Moveable::AddInstantAcceleration(const Acceleration& acceleration)
{
    m_InstantAcceleration += acceleration;
}

const Acceleration& Moveable::GetInstantAcceleration() const
{
    return m_InstantAcceleration;
}

void Moveable::SetNewSpeed(const Speed& speed)
{
    m_NewSpeed = speed;
    TestSpeedContraints();
}

void Moveable::SetNewPosition(const Point& point)
{
    m_NewPosition = point;
}

void Moveable::ResetNewPosition()
{
    m_NewPosition = m_Position;
}

void Moveable::SetNewAcceleration(const Acceleration& acceleration)
{
    m_NewAcceleration = acceleration;
}

void Moveable::SetMaxSpeed(const Speed& speed)
{
    m_MaxSpeed = speed;
}

const Speed& Moveable::GetMaxSpeed() const
{
    return m_MaxSpeed;
}

